// 创建朝代节点
CREATE (dynasty:Dynasty {name: "唐朝", startYear: 618, endYear: 907, capital: "长安、洛阳"})

// 创建唐朝主要皇帝节点
CREATE (liyuan:Emperor {name: "李渊", posthumousTitle: "唐高祖", reignStart: 618, reignEnd: 626, eraName: "武德"})
CREATE (li_shimin:Emperor {name: "李世民", posthumousTitle: "唐太宗", reignStart: 626, reignEnd: 649, eraName: "贞观", achievement: "贞观之治"})
CREATE (li_zhi:Emperor {name: "李治", posthumousTitle: "唐高宗", reignStart: 649, reignEnd: 683, eraName: "永徽、显庆等"})
CREATE (wu_zetian:Emperor {name: "武则天", posthumousTitle: "则天大圣皇帝", reignStart: 690, reignEnd: 705, eraName: "天授、证圣等", note: "中国唯一女皇帝，改国号为周"})
CREATE (li_xian:Emperor {name: "李显", posthumousTitle: "唐中宗", reignStart: 705, reignEnd: 710, eraName: "神龙、景龙"})
CREATE (li_longji:Emperor {name: "李隆基", posthumousTitle: "唐玄宗", reignStart: 712, reignEnd: 756, eraName: "开元、天宝", achievement: "开元盛世", note: "后期爆发安史之乱"})
CREATE (li_heng:Emperor {name: "李亨", posthumousTitle: "唐肃宗", reignStart: 756, reignEnd: 762, eraName: "至德、乾元"})
CREATE (li_yu:Emperor {name: "李煜", posthumousTitle: "唐后主", reignStart: 961, reignEnd: 975, note: "南唐最后一位君主，著名词人"})  // 注：南唐为唐朝后续政权，此处作为补充

// 建立皇帝与朝代的归属关系
MATCH (e:Emperor), (d:Dynasty {name: "唐朝"})
WHERE e.name IN ["李渊", "李世民", "李治", "武则天", "李显", "李隆基", "李亨"]
CREATE (e)-[r:BELONG_TO]->(d)
SET r.relationship = "属于该朝代"

// 建立皇位继承关系
MATCH (a:Emperor {name: "李渊"}), (b:Emperor {name: "李世民"})
CREATE (a)-[r:SUCCESSION {type: "禅让", note: "玄武门之变后李渊退位"}]->(b)

MATCH (a:Emperor {name: "李世民"}), (b:Emperor {name: "李治"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李治为李世民第九子"}]->(b)

MATCH (a:Emperor {name: "李治"}), (b:Emperor {name: "武则天"})
CREATE (a)-[r:SUCCESSION {type: "夫妻继承", note: "武则天为李治皇后，后称帝"}]->(b)

MATCH (a:Emperor {name: "武则天"}), (b:Emperor {name: "李显"})
CREATE (a)-[r:SUCCESSION {type: "母子继承", note: "李显为武则天第三子，复辟唐朝"}]->(b)

MATCH (a:Emperor {name: "李显"}), (b:Emperor {name: "李隆基"})
CREATE (a)-[r:SUCCESSION {type: "叔侄继承", note: "李隆基为李显侄子，通过唐隆政变继位"}]->(b)

MATCH (a:Emperor {name: "李隆基"}), (b:Emperor {name: "李亨"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "安史之乱中李亨继位，尊李隆基为太上皇"}]->(b)

// 建立亲属关系（补充家庭关系）
MATCH (a:Emperor {name: "李世民"}), (b:Emperor {name: "李渊"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子", note: "李世民为李渊次子"}]->(b)

MATCH (a:Emperor {name: "李治"}), (b:Emperor {name: "李世民"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李显"}), (b:Emperor {name: "李治"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李显"}), (b:Emperor {name: "武则天"})
CREATE (a)-[r:FAMILY_RELATION {type: "母子"}]->(b)

MATCH (a:Emperor {name: "李隆基"}), (b:Emperor {name: "李亨"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)


// 补充创建唐代中后期皇帝节点
CREATE (li_yu:Emperor {name: "李豫", posthumousTitle: "唐代宗", reignStart: 762, reignEnd: 779, eraName: "宝应、广德等", note: "平定安史之乱收尾"})
CREATE (li_shi:Emperor {name: "李适", posthumousTitle: "唐德宗", reignStart: 779, reignEnd: 805, eraName: "建中、兴元等", note: "推行两税法"})
CREATE (li_song:Emperor {name: "李诵", posthumousTitle: "唐顺宗", reignStart: 805, reignEnd: 805, eraName: "永贞", note: "在位仅8个月，推行永贞革新"})
CREATE (li_chun:Emperor {name: "李纯", posthumousTitle: "唐宪宗", reignStart: 805, reignEnd: 820, eraName: "元和", achievement: "元和中兴"})
CREATE (li_heng2:Emperor {name: "李恒", posthumousTitle: "唐穆宗", reignStart: 820, reignEnd: 824, eraName: "长庆", note: "耽于享乐，藩镇复叛"})
CREATE (li_zhan:Emperor {name: "李湛", posthumousTitle: "唐敬宗", reignStart: 824, reignEnd: 826, eraName: "宝历", note: "年少继位，被宦官杀害"})
CREATE (li_ang:Emperor {name: "李昂", posthumousTitle: "唐文宗", reignStart: 826, reignEnd: 840, eraName: "大和、开成", note: "甘露之变失败，宦官专权加剧"})
CREATE (li_yan:Emperor {name: "李炎", posthumousTitle: "唐武宗", reignStart: 840, reignEnd: 846, eraName: "会昌", achievement: "会昌中兴，打击佛教"})
CREATE (li_chen:Emperor {name: "李忱", posthumousTitle: "唐宣宗", reignStart: 846, reignEnd: 859, eraName: "大中", achievement: "大中之治，人称小太宗"})
CREATE (li_cui:Emperor {name: "李漼", posthumousTitle: "唐懿宗", reignStart: 859, reignEnd: 873, eraName: "咸通", note: "朝政腐败，农民起义爆发"})
CREATE (li_xuan:Emperor {name: "李儇", posthumousTitle: "唐僖宗", reignStart: 873, reignEnd: 888, eraName: "乾符、广明等", note: "黄巢起义时期在位，流亡成都"})
CREATE (li_ye:Emperor {name: "李晔", posthumousTitle: "唐昭宗", reignStart: 888, reignEnd: 904, eraName: "龙纪、大顺等", note: "试图重振皇权失败，被朱温控制"})
CREATE (li_zhu:Emperor {name: "李柷", posthumousTitle: "唐哀帝", reignStart: 904, reignEnd: 907, eraName: "天祐", note: "唐朝末代皇帝，被朱温所废"})

// 建立补充皇帝与唐朝的归属关系
MATCH (e:Emperor), (d:Dynasty {name: "唐朝"})
WHERE e.name IN ["李豫", "李适", "李诵", "李纯", "李恒", "李湛", "李昂", "李炎", "李忱", "李漼", "李儇", "李晔", "李柷"]
CREATE (e)-[r:BELONG_TO]->(d)
SET r.relationship = "属于该朝代"

// 补充皇位继承关系（衔接之前的皇帝）
MATCH (a:Emperor {name: "李亨"}), (b:Emperor {name: "李豫"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李豫为李亨长子"}]->(b)

MATCH (a:Emperor {name: "李豫"}), (b:Emperor {name: "李适"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李适为李豫长子"}]->(b)

MATCH (a:Emperor {name: "李适"}), (b:Emperor {name: "李诵"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李诵为李适长子"}]->(b)

MATCH (a:Emperor {name: "李诵"}), (b:Emperor {name: "李纯"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李诵退位后李纯继位"}]->(b)

MATCH (a:Emperor {name: "李纯"}), (b:Emperor {name: "李恒"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李恒为李纯第三子"}]->(b)

MATCH (a:Emperor {name: "李恒"}), (b:Emperor {name: "李湛"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李湛为李恒长子"}]->(b)

MATCH (a:Emperor {name: "李湛"}), (b:Emperor {name: "李昂"})
CREATE (a)-[r:SUCCESSION {type: "兄弟继承", note: "李昂为李恒次子，李湛被弑后继位"}]->(b)

MATCH (a:Emperor {name: "李昂"}), (b:Emperor {name: "李炎"})
CREATE (a)-[r:SUCCESSION {type: "兄弟继承", note: "李炎为李恒第五子，宦官拥立继位"}]->(b)

MATCH (a:Emperor {name: "李炎"}), (b:Emperor {name: "李忱"})
CREATE (a)-[r:SUCCESSION {type: "叔侄继承", note: "李忱为唐宪宗第十三子，李炎堂弟"}]->(b)

MATCH (a:Emperor {name: "李忱"}), (b:Emperor {name: "李漼"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李漼为李忱长子"}]->(b)

MATCH (a:Emperor {name: "李漼"}), (b:Emperor {name: "李儇"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李儇为李漼第五子"}]->(b)

MATCH (a:Emperor {name: "李儇"}), (b:Emperor {name: "李晔"})
CREATE (a)-[r:SUCCESSION {type: "兄弟继承", note: "李晔为李漼第七子，李儇弟"}]->(b)

MATCH (a:Emperor {name: "李晔"}), (b:Emperor {name: "李柷"})
CREATE (a)-[r:SUCCESSION {type: "父子继承", note: "李柷为李晔第九子，朱温拥立的傀儡皇帝"}]->(b)

// 补充主要亲属关系（父子/兄弟）
MATCH (a:Emperor {name: "李豫"}), (b:Emperor {name: "李亨"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李适"}), (b:Emperor {name: "李豫"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李诵"}), (b:Emperor {name: "李适"}) 
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李纯"}), (b:Emperor {name: "李诵"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李恒"}), (b:Emperor {name: "李纯"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

// 兄弟关系（李恒之子）
MATCH (a:Emperor {name: "李湛"}), (b:Emperor {name: "李昂"})
CREATE (a)-[r:FAMILY_RELATION {type: "兄弟", note: "李湛为兄，李昂为弟"}]->(b)

MATCH (a:Emperor {name: "李昂"}), (b:Emperor {name: "李炎"})
CREATE (a)-[r:FAMILY_RELATION {type: "兄弟", note: "李昂为兄，李炎为弟"}]->(b)

// 李忱与李纯的父子关系（唐宣宗是唐宪宗之子）
MATCH (a:Emperor {name: "李忱"}), (b:Emperor {name: "李纯"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

// 晚唐父子关系
MATCH (a:Emperor {name: "李漼"}), (b:Emperor {name: "李忱"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李儇"}), (b:Emperor {name: "李漼"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李晔"}), (b:Emperor {name: "李漼"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)

MATCH (a:Emperor {name: "李柷"}), (b:Emperor {name: "李晔"})
CREATE (a)-[r:FAMILY_RELATION {type: "父子"}]->(b)


MATCH p=(:Dynasty {name:"唐朝"})<-[:BELONG_TO]-(:Emperor)-[*]->(:Emperor)
RETURN p